import { ProductStatus } from '@/enum/product'
import { message, get, post } from '@/utils'

/**
 * 产品相关
 */
export default () => {
  const info = ref({} as any as Product)
  const getInfo = (id, cb?: Function) => {
    get({ url: '/server-shop/sys-seller/goods/info', data: { id } }, ({ data }) => {
      info.value = data

      cb?.()
    })
  }
  /**修改商品状态
   */
  const changeStatus = (query: { ids: string }, status, cb?: Function) => {
    if (status == ProductStatus.上架中) takeDown(query, () => cb?.())
    else shelve(query, () => cb?.())
  }
  /**上架 */
  const shelve = (query: { ids: string }, cb?: Function) => {
    post({ url: '/server-shop/sys-seller/goods/up?ids=' + query.ids, data: query }, ({ code, msg }) => {
      cb?.(code == 200)
      message({
        type: code == 200 ? 'success' : 'error',
        content: msg,
      })
    })
  }
  /**下架 */
  const takeDown = (query: { ids: string }, cb?: Function) => {
    post({ url: '/server-shop/sys-seller/goods/down?ids=' + query.ids, data: query }, ({ code, msg }) => {
      cb?.(code == 200)
      message({
        type: code == 200 ? 'success' : 'error',
        content: msg,
      })
    })
  }
  /**
   * 删除商品
   * @param id
   */
  const remove = (id, cb?: Function) => {
    post({ url: '/server-shop/sys-seller/goods/remove?ids=' + id }, ({ code, msg }) => {
      cb?.(code == 200)
      message({
        type: code == 200 ? 'success' : 'error',
        content: msg,
      })
    })
  }
  /**
   * 新增商品
   * @param query
   * @param cb
   */
  const add = (query, cb?: Function) => {
    post({ url: '/server-shop/sys-seller/goods/save', data: query }, ({ code, msg }) => {
      cb?.(code == 200)
      message({
        type: code == 200 ? 'success' : 'error',
        content: msg,
      })
    })
  }
  /**
   * 编辑商品
   * @param query
   * @param cb
   */
  const update = (query, cb?: Function) => {
    post({ url: '/server-shop/sys-seller/goods/update', data: query }, ({ code, msg }) => {
      cb?.(code == 200)
      message({
        type: code == 200 ? 'success' : 'error',
        content: msg,
      })
    })
  }
  return {
    info,
    getInfo,
    changeStatus,
    remove,
    add,
    update,
  }
}
